import functools
from app.models import *
from app import app
import time
from flask import make_response,request,jsonify,redirect,url_for

def admin_require(func):
    @functools.wraps(func)
    def decorator(*args,**kwargs):
        aid = request.cookies.get("ad_uuid")
        if not aid:
            resp = make_response(jsonify({
                'code': -101,
                'msg': '请登录！'
            }))

            resp.delete_cookie("ad_uuid")
            return resp

        acookie = Admin_Cookies.query.filter_by(cookies=aid).first()
        if not acookie:
            resp = make_response(jsonify({
                'code': -101,
                'msg': '请登录！'
            }))
            resp.delete_cookie("ad_uuid")
            return resp
        if datetime.datetime.now() < acookie.endTime:
            admin = acookie.admin
            if admin.flag != 1:
                resp = {
                    'code':-108,
                    'msg':'账号被封停处理！',
                }
                return resp
            acookie.endTime = (datetime.datetime.now()+app.config['PEMEMBER_COOKIE_DURATION']).timetuple()
            db.session.commit()
            return func(*args, **kwargs,aid = admin)

        else:
            resp = make_response(jsonify({
                'code': -101,
                'msg': '请登录！'
            }))

            resp.delete_cookie("ad_uuid")

            return resp

    return decorator

